package com.cskaoyan.market.service.Impl;

import com.cskaoyan.market.bo.UpdateCategoryBo;
import com.cskaoyan.market.db.domain.MarketCategory;
import com.cskaoyan.market.db.domain.MarketCategoryExample;
import com.cskaoyan.market.db.mapper.MarketCategoryMapper;
import com.cskaoyan.market.service.MarketCategoryService;
import com.cskaoyan.market.vo.MarketCategoryL1Vo;
import com.cskaoyan.market.vo.MarketCategoryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: MarketCategoryServiceImpl
 * @Description: MarketCategoryService的实现类
 * @Author: 鲸落
 * @Date: 2024/05/06/21:56
 * @Version: 1.0
 */
@Service
public class MarketCategoryServiceImpl implements MarketCategoryService {
    @Autowired
    MarketCategoryMapper categoryMapper;
    @Override
    public List<MarketCategoryVo> list() {
        Map<Integer, List<MarketCategory>> categoryMap = groupByPidCategory();
        List<MarketCategoryVo> marketCategoryVoList = new ArrayList<>();    // 返回的最终结果
        List<MarketCategory> l1s = categoryMap.get(0);
        for (MarketCategory l1 : l1s) {
            List<MarketCategoryVo> childrenOfL1 = new ArrayList<>();
            // 向最终结果中填充的数据
            MarketCategoryVo l1Vo = new MarketCategoryVo(l1.getId(), l1.getName(), l1.getKeywords(), l1.getDesc(),
                    l1.getIconUrl(), l1.getPicUrl(), l1.getLevel(), childrenOfL1);
            marketCategoryVoList.add(l1Vo);
            // 当前一级类目下的所有二级类目
            List<MarketCategory> l2s = categoryMap.get(l1.getId());
            if (l2s != null && l2s.size() > 0) { // 判断是否为空
                for (MarketCategory l2 : l2s) {
                    childrenOfL1.add(new MarketCategoryVo(l2.getId(), l2.getName(), l2.getKeywords(), l2.getDesc(),
                            l2.getIconUrl(), l2.getPicUrl(), l2.getLevel(),null));
                }
            }
        }
        return marketCategoryVoList;
    }

    @Override
    public List<MarketCategoryL1Vo> l1() {
        Map<Integer, List<MarketCategory>> categoryMap = groupByPidCategory();
        List<MarketCategory> l1s = categoryMap.get(0);
        List<MarketCategoryL1Vo> marketCategoryL1VoList = new ArrayList<>();
        for (MarketCategory l1 : l1s) {
            Integer value = l1.getId();
            String label = l1.getName();
            MarketCategoryL1Vo l1Vo = new MarketCategoryL1Vo();
            l1Vo.setValue(value + "");
            l1Vo.setLabel(label);
            marketCategoryL1VoList.add(l1Vo);
        }
        return marketCategoryL1VoList;
    }

    @Override
    public MarketCategory create(MarketCategory createCategory) {
        createCategory.setAddTime(LocalDateTime.now());
        createCategory.setUpdateTime(LocalDateTime.now());
        categoryMapper.insertSelective(createCategory);
        return createCategory;
    }

    @Override
    public void delete(MarketCategoryVo requestCategoryVo) {
        MarketCategoryExample categoryExample = new MarketCategoryExample();
        MarketCategoryExample.Criteria criteria = categoryExample.createCriteria();
        // 删除之前先判断一下是一级类目还是二级类目
        List<MarketCategoryVo> children = requestCategoryVo.getChildren();
        if(children != null && children.size() > 0){
            // 当前删除的类目是一级类目，并且下面有二级类目，要先删除二级类目
            criteria.andPidEqualTo(requestCategoryVo.getId());
            categoryMapper.deleteByExample(categoryExample);
        }
        // 其余情况直接按id删除即可
        categoryMapper.deleteByPrimaryKey(requestCategoryVo.getId());
    }

    @Override
    public void update(UpdateCategoryBo updateCategoryBo) {
        // 更新之前先查找到要更新的数据
        MarketCategory updateCategory = categoryMapper.selectByPrimaryKey(updateCategoryBo.getId());
        updateCategory.setDesc(updateCategoryBo.getDesc());
        updateCategory.setIconUrl(updateCategoryBo.getIconUrl());
        updateCategory.setKeywords(updateCategoryBo.getKeywords());
        updateCategory.setLevel(updateCategoryBo.getLevel());
        updateCategory.setName(updateCategoryBo.getName());
        updateCategory.setPicUrl(updateCategoryBo.getPicUrl());
        updateCategory.setUpdateTime(LocalDateTime.now());
        // 如果pid=null，则说明一定是一改一类型的，pid不做更改；其他情况pid需要更改
        if(updateCategoryBo.getPid() != null){
            updateCategory.setPid(updateCategoryBo.getPid());
        }
        //
        categoryMapper.updateByPrimaryKey(updateCategory);
    }

    // 根据pid进行分组
    private Map<Integer,List<MarketCategory>> groupByPidCategory(){
        List<MarketCategory> marketCategories = categoryMapper.selectByExample(new MarketCategoryExample());
        Map<Integer, List<MarketCategory>> categoryMap = new HashMap<>();
        for (MarketCategory category : marketCategories) {
            Integer pid = category.getPid();
            //session里面的购物车思想是一致的
            //先尝试从map中获取，对于第一次查询，肯定是null，创建一个list，放入map中
            List<MarketCategory> list = categoryMap.get(pid);
            if(list == null){
                list = new ArrayList<>();
            }
            list.add(category);
            categoryMap.put(pid, list);
        }
        return categoryMap;
    }

}
